package baiqitun.netfiles.controller;

import baiqitun.akutils.PathUtil;
import baiqitun.akutils.StreamUtil;
import baiqitun.netfiles.config.properties.SupportSuffixProperties;
import baiqitun.netfiles.entity.SysDirectory;
import baiqitun.netfiles.entity.SysFile;
import baiqitun.netfiles.exception.NetFilesException;
import baiqitun.netfiles.service.SysDirectoryService;
import baiqitun.netfiles.service.SysFileService;
import baiqitun.netfiles.util.ThymeleafUtil;
import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author kedaji
 */
@Controller
public class BrowseController {
    @Autowired
    private SupportSuffixProperties supportSuffixProperties;
    @Autowired
    private SysFileService sysFileService;
    @Autowired
    private SysDirectoryService sysDirectoryService;

    /**
     * 跳转目录
     * @param id 目录id
     * @return 视图
     */
    @GetMapping("/directory/{id}")
    public ModelAndView directory(@PathVariable("id") String id){
        ModelAndView mav = ThymeleafUtil.createDefaultModelAndView();
        SysDirectory directory = sysDirectoryService.dir(id);
        mav.addObject("title", "远程文件浏览器-浏览目录");
        mav.addObject("roots", sysDirectoryService.listRoot());
        mav.addObject("directory", directory);
        mav.setViewName("index");

        return mav;
    }

    /**
     * 资源
     * @param id 资源id
     * @param response 响应
     */
    @GetMapping("/resource/{id}")
    public void resource(@PathVariable("id") String id, HttpServletResponse response) {
        String p = sysFileService.getMappingPath(id);
        Path path = Paths.get(p);
        try {
            byte[] bytes = Files.readAllBytes(path);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } catch (IOException e) {
            throw new NetFilesException(e);
        }
    }

    /**
     * 跳转到图片查看页面
     * @param id 图片id
     * @return 视图
     */
    @GetMapping("/image/{id}")
    public ModelAndView image(@PathVariable("id") String id){
        ModelAndView mav = ThymeleafUtil.createDefaultModelAndView();
        mav.addObject("file", sysFileService.get(id));

        mav.setViewName("view/image");
        return mav;
    }

    /**
     * 切换到图片浏览模式
     * @param id 目录id
     * @return 视图
     */
    @GetMapping("/images/{id}")
    public ModelAndView images(@PathVariable("id") String id){

        SysDirectory directory = sysDirectoryService.get(id);
        String parentPath = sysDirectoryService.getMappingPath(id);
        Stream<Path> imageStream = PathUtil.filesBySuffix(Paths.get(parentPath), supportSuffixProperties.getImages());

        List<Path> paths = StreamUtil.toList(imageStream);
        List<SysFile> files = new LinkedList<>();

        for (Path tempPath: paths){
            SysFile file = sysFileService.getByPath(tempPath.toString());
            files.add(file);
        }


        ModelAndView mav = ThymeleafUtil.createDefaultModelAndView();
        mav.addObject("directory", directory);
        mav.addObject("images", files);

        mav.setViewName("view/images");
        return mav;
    }

    /**
     * 跳转到视频查看页面页面
     * @param id 视频id
     * @return 视图
     */
    @GetMapping("/video/{id}")
    public ModelAndView video(@PathVariable("id") String id){
        ModelAndView mav = ThymeleafUtil.createDefaultModelAndView();

        mav.addObject("file", sysFileService.get(id));
        mav.setViewName("view/video");
        return mav;
    }


    /**
     * 跳转到视频查看页面页面
     * @param id 视频id
     * @return 视图
     */
    @GetMapping("/text/{id}")
    public ModelAndView text(@PathVariable("id") String id, String charset){
        ModelAndView mav = ThymeleafUtil.createDefaultModelAndView();

        mav.addObject("file", sysFileService.get(id));
        String mappingPath = sysFileService.getMappingPath(id);
        String content = "";
        if (charset == null){
            content = FileUtil.readString(mappingPath, Charset.forName("GBK"));
        } else {
            content = FileUtil.readString(mappingPath, Charset.forName(charset));
        }
        mav.addObject("content", content);
        mav.setViewName("view/text");
        return mav;
    }

    /**
     * 跳转到未知文件浏览页面
     * @param id 文件id
     * @return 视图
     */
    @GetMapping("/unknown/{id}")
    public ModelAndView download(@PathVariable("id") String id){
        ModelAndView mav = ThymeleafUtil.createDefaultModelAndView();

        mav.addObject("file", sysFileService.get(id));
        mav.setViewName("view/unknown");
        return mav;
    }
}
